﻿using BsForApi;
using BsForApi.Model;
using HCDV;
using HCDV.HCSP;
using hcrainMvc.ashx.ywfunction;
using hrainwebbase.ashx.spfunction;
using hrainwebbase.basecs;
//using hcrainMvc.BLL;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Http;
using System.Web.Script.Serialization;

namespace hcrainMvc.Controllers
{
    public class HcAPIController : ApiController
    {
        /// <summary>
        /// 从字符串中获取 非数值码与数值及长度 
        /// 如  A0012F0009 返回 A0012F, 9 ,4(0009)
        /// 如  A0012F0009A  返回 A0012F0009A, 0, 0(空)
        /// </summary>
        /// <param name="codeinfo"></param>
        /// <returns></returns>
        [ActionName("GetCodeNum")]
        [HttpGet]
        public object GetCodeNum(string codeinfo)
        {
            var info = simplefunction.hcrainFunction_getCodeNum(codeinfo);
            return new { codestring = info[0], codenum = info[1], codenuml = info[2] };

        }
        [HttpPost]
        [ActionName("PostPickSend")]
        public string PostPickSend(ApiIn<List<PickSendInput>> input)
        {
            List<PickSendInput> list = input.Info;
            try
            {
                foreach (var re in list)
                {
                    var arg = new ApiIn<PickSendInput>();
                    arg.LoginInfo = input.LoginInfo;
                    arg.OrgName = input.OrgName;
                    arg.Info = re;
                    BsForApi.JDBs.PostPickSend(arg, false);
                }
                if (input.Info.Count>0) BsForApi.JDBs.GetReSetOutInfoToCenter(input.LoginInfo.UserOrgId, input.Info[0].sendday);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return "处理成功";
        }
        [ActionName("PostGetInfo")]
        [HttpPost]
        public string PostGetInfo(Doget codelist)
        {
            DataTable dt = null;
            using (dbsimple db = new dbsimple())
            {
                dt = db.Query(codelist.sql);
            }
            return ToJson(dt);
        }
        [ActionName("PostDoInfo")]
        [HttpPost]
        public int PostDoInfo(Doget codelist)
        {

            DataTable dt = null;
            using (dbsimple db = new dbsimple())
            {
                return db.exe(codelist.sql);
            }

        }

        public static string ToJson(DataTable dt)
        {
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
            ArrayList arrayList = new ArrayList();
            foreach (DataRow dataRow in dt.Rows)
            {
                Dictionary<string, object> dictionary = new Dictionary<string, object>();  //实例化一个参数集合
                foreach (DataColumn dataColumn in dt.Columns)
                {
                    dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToString());
                }
                arrayList.Add(dictionary); //ArrayList集合中添加键值
            }

            return javaScriptSerializer.Serialize(arrayList);  //返回一个json字符串
        }

        //#region 转换为string字符串类型
        ///// <summary>
        /////  转换为string字符串类型
        ///// </summary>
        ///// <param name="s">获取需要转换的值</param>
        ///// <param name="format">需要格式化的位数</param>
        ///// <returns>返回一个新的字符串</returns>
        //public static string ToStr(this object s, string format = "")
        //{
        //    string result = "";
        //    try
        //    {
        //        if (format == "")
        //        {
        //            result = s.ToString();
        //        }
        //        else
        //        {
        //            result = string.Format("{0:" + format + "}", s);
        //        }
        //    }
        //    catch
        //    {
        //    }
        //    return result;
        //}
        //#endregion
        /// <summary>
        /// Json 字符串 转换为 DataTable数据集合
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static DataTable ToDataTable(string json)
        {
            DataTable dataTable = new DataTable();  //实例化
            DataTable result;
            try
            {
                JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
                javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
                ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
                if (arrayList.Count > 0)
                {
                    foreach (Dictionary<string, object> dictionary in arrayList)
                    {
                        if (dictionary.Keys.Count<string>() == 0)
                        {
                            result = dataTable;
                            return result;
                        }
                        if (dataTable.Columns.Count == 0)
                        {
                            foreach (string current in dictionary.Keys)
                            {
                                dataTable.Columns.Add(current, dictionary[current].GetType());
                            }
                        }
                        DataRow dataRow = dataTable.NewRow();
                        foreach (string current in dictionary.Keys)
                        {
                            dataRow[current] = dictionary[current];
                        }

                        dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
                    }
                }
            }
            catch
            {
            }
            result = dataTable;
            return result;
        }
    }
    public class Doget
    {
        public string jdid { get; set; }
        public string jdkey { get; set; }
        public string sql { get; set; }
    }
}
